//
//  YBCode.m
//  Cocoachina
//
//  Created by 游兵 on 2017/9/15.
//  Copyright © 2017年 YBing. All rights reserved.
//

#import "YBCode.h"
#import "NSUserDefaults+Extension.h"
#import "NSString+MD5Addition.h"

#define kYBCodeWidth ((kScreenW-24)/2.0)

@implementation YBCode

- (CGFloat)imgscale
{
//    if (_imgscale==0) {
//        _imgscale = [NSUserDefaults getFloatForKey:[_url stringFromMD5]];
//    }
//    if (_imgscale==0) {
        return 2/3.0;
//    }
    return _imgscale;
}

- (CGFloat)imgH
{
    if (_imgH==0) {
        _imgH = kYBCodeWidth/(self.imgscale);
    }
    return _imgH;
}

- (CGFloat)titleH
{
    if (_titleH==0) {
        _titleH = [self.title boundingRectWithSize:CGSizeMake(kYBCodeWidth-16, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;;
    }
    return _titleH;
}

- (CGFloat)descH
{
    if (_descH==0) {
        _descH = [self.desc boundingRectWithSize:CGSizeMake(kYBCodeWidth-16, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;
    }
    return _descH;
}

- (CGFloat)numH
{
    if (_numH==0) {
        _numH = 17;
    }
    return _numH;
}

- (CGFloat)height
{
    if (_height==0) {
        _height = self.imgH+8+self.titleH+8+self.numH+8+self.descH;
    }
    return _height;
}

- (void)setImg:(NSString *)img
{
    if ([img hasPrefix:@"http"]) {
        _img = [img copy];
    } else {
        _img = [[NSString stringWithFormat:@"%@%@", @"http://code.cocoachina.com/", img] copy];
    }
}

@end
